package jmine.tec.proxy;

import java.util.concurrent.locks.Lock;

import jmine.tec.proxy.chain.ChainElement;
import jmine.tec.proxy.chain.InvocationChain;

/**
 * @author takeshi
 * 
 */
public class LockChainElement implements ChainElement {

    private final Lock lock;

    /**
     * 
     * @param lock
     *            lock
     */
    public LockChainElement(final Lock lock) {
        super();
        if (lock == null) {
            throw new NullPointerException("Lock cannot be null!");
        }
        this.lock = lock;
    }

    /**
     * @param chain
     *            chain
     * @return o
     * @throws Throwable
     *             t
     * @see br.com.maps.util.proxy.chain.ChainElement#aroundInvoke(br.com.maps.util.proxy.chain.InvocationChain)
     */
    public Object aroundInvoke(final InvocationChain chain) throws Throwable {
        lock.lock();
        try {
            return chain.proceed();
        } finally {
            lock.unlock();
        }
    }

}
